let qaid;
let payway;
onload = function(){
	liang();
	daohanglan();
	var sheng=document.querySelector("#sheng");

	let html=``
	for (var i = 0; i < citys.length; i++) {
		html+=`<div onclick="clicksheng(`+i+`)"  class="sheng" style="background-color: #dddcdc;border-radius: 5px;padding: 10px;">`+(citys[i])["n"]+`</div>`;
	}
	// citys.forEach(c=>{
	// 	html+=`<div  class="sheng" style="background-color: #0381fe;border-radius: 5px;padding: 10px;">`+c["n"]+`</div>`;
	// });
	sheng.innerHTML=html;
	var shengdiv=document.querySelectorAll(".sheng");

	addressstyle();



	document.querySelector("#search").addEventListener("keydown",keyboard)
	document.querySelector("#search").addEventListener("input",searchtips)
	document.querySelector(".searchtip").addEventListener("mouseleave",function (){
		document.querySelector(".searchtip").style.display="none"
	})

	document.querySelectorAll(".xlli").forEach(x=>{
		x.addEventListener("mouseover",xllijdbc);
	})
	a =document.querySelector(".showNavtwoson").innerHTML;
	document.querySelector(".newtuijian").addEventListener("mouseover",function (){
		document.querySelector(".showNavtwoson").innerHTML=a;
	})
	document.querySelectorAll(".gnb_nav_center").forEach(x=>{
		x.addEventListener("mouseover",function (){
			document.querySelector(".showNavtwoson").innerHTML=a;
		})
	})

	document.querySelectorAll(".xlli").forEach(x=>{
		x.addEventListener("click",function (){
			let xid = parseInt(event.target.getAttribute("id"));
			location.href="/goodsList?xid="+xid+"&show="+1;
		})
	})


}


function keyboard(){
	let keycode = event.keyCode;
	if (keycode == 13){
		search();
	}
}

function search(){
	let searchvalue = document.querySelector("#search").value;
	if(searchvalue==""){
		event.preventDefault();
	}else {
		location.href="/index?v=search&searchvalue="+searchvalue;
	}

}

function searchtips(){
	if (document.querySelector("#search").value==""){
		document.querySelector(".searchtip").style.display="none"
	}else {
		document.querySelector(".searchtip").style.display="block"
	}
	let searchvalue = document.querySelector("#search").value;
	$.ajax({
		url:"/index?v=searchtip",
		type:"post",
		data:{
			searchvalue
		},
		dataType:"json",
		success:(resp=>{
			if (resp.length>4){
				document.querySelector(".searchtip").style.height="80px";
				document.querySelector(".searchtip").style.overflowY="scroll";
			}else if (resp.length==0){
				document.querySelector(".searchtip").style.height="20px";
				document.querySelector(".searchtip").style.overflowY="";
			}else if(resp.length<4){
				document.querySelector(".searchtip").style.height="auto";
				document.querySelector(".searchtip").style.overflowY="";
			}
			let tipsbox = document.querySelector(".searchtip");
			let html=``;
			resp.forEach(r=>{
				html+=`<div onclick="bindfuture()">`+r.sname+`</div>`
			})
			tipsbox.innerHTML=html;
		})
	})
}

function bindfuture(){
	let divname = event.currentTarget.innerText;
	location.href="/index?v=search&searchvalue="+divname;
}


function xllijdbc(){
	let xid = parseInt(event.currentTarget.getAttribute("id"));
	$.ajax({
		url:"/index?v=searchli",
		type:"post",
		data:{
			xid
		},
		success:((resp)=>{
			let r = JSON.parse(resp);
			let showNavtwoson = document.querySelector(".showNavtwoson");
			let html = ``;
			r.forEach(x=>{
				x.commodities.forEach(y=>{
					html+=`<li onclick="chuandetail(${y.sid},${y.lid})">`+x.lname+`</li>`
				})
			})
			showNavtwoson.innerHTML=html;
		})
	})
}

function chuandetail(params1,params2){
	location.href="/productDetails?sid="+params1+"&lid="+params2;
}


function keyboard(){
	let keycode = event.keyCode;
	if (keycode == 13){
		search();
	}
}

function search(){
	let searchvalue = document.querySelector("#search").value;
	if(searchvalue==""){
		event.preventDefault();
	}else {
		location.href="/index?v=search&searchvalue="+searchvalue;
	}

}

function searchtips(){
	if (document.querySelector("#search").value==""){
		document.querySelector(".searchtip").style.display="none"
	}else {
		document.querySelector(".searchtip").style.display="block"
	}
	let searchvalue = document.querySelector("#search").value;
	$.ajax({
		url:"/index?v=searchtip",
		type:"post",
		data:{
			searchvalue
		},
		dataType:"json",
		success:(resp=>{
			if (resp.length>4){
				document.querySelector(".searchtip").style.height="80px";
				document.querySelector(".searchtip").style.overflowY="scroll";
			}else if (resp.length==0){
				document.querySelector(".searchtip").style.height="20px";
				document.querySelector(".searchtip").style.overflowY="";
			}else if(resp.length<4){
				document.querySelector(".searchtip").style.height="auto";
				document.querySelector(".searchtip").style.overflowY="";
			}
			let tipsbox = document.querySelector(".searchtip");
			let html=``;
			resp.forEach(r=>{
				html+=`<div onclick="bindfuture()">`+r.sname+`</div>`
			})
			tipsbox.innerHTML=html;
		})
	})
}

function bindfuture(){
	let divname = event.currentTarget.innerText;
	location.href="/index?v=search&searchvalue="+divname;
}
function bindfuturenew(){
	let divname = event.currentTarget.innerText;
	let divnames = divname.substr(0,divname.length-1);
	location.href="/index?v=search&searchvalue="+divnames;
}

function xllijdbc(){
	let xid = parseInt(event.currentTarget.getAttribute("id"));
	$.ajax({
		url:"/index?v=searchli",
		type:"post",
		data:{
			xid
		},
		success:((resp)=>{
			let r = JSON.parse(resp);
			let showNavtwoson = document.querySelector(".showNavtwoson");
			let html = ``;
			r.forEach(y=>{
				html+=`<li onclick="chuandetail(${y.sid},${y.lid})">`+y.sname+`</li>`
			})
			showNavtwoson.innerHTML=html;
		})
	})
}




function Login(){
	let login = document.querySelector("#Login");
	let levitation = document.querySelector("#levitation");

	login.onmouseenter = function (){
		levitation.style.display = "block";
	}
	levitation.onmouseleave = function (){
		levitation.style.display = "none";
	}
}
function isnull(){
	location.href="/login"
}
function iscart(){
	location.href="/goods"
}
function onclickisdefault(){
	document.querySelectorAll(".cad-address-input").forEach(x=>{
		x.addEventListener("input",function (){
			let m = event.target.parentNode;
			console.log(m.childNodes[1]);
			let val = m.childNodes[1];
			let a = val.value;
			$.ajax({
				url:"/orderXq?v=Upisdefault",
				type:"post",
				data:{
					a
				},
				success:function (r){
					addressstyle();
					// $.ajax({
					// 	url:"/orderXq",
					// 	type:"post",
					// 	data:{
					//
					// 	},
					// 	success:(y=>{
					//
					// 	})
					// })
				}
			})
		})
	})
}
function addressstyle(){
	console.log(111111);
	$.ajax({
		url:"/orderXq?v=addressStyle",
		type:"post",
		data:{

		},
		success:(r=>{
			if (r!=null) {

				r.forEach(x => {
					if (x.isdefault==1) {
						let newedit = document.querySelector(".order-new-edit");
						let adedit = document.querySelector(".order-edit");
						var adname = document.querySelector(".addressname");
						var adphone = document.querySelector(".addressphone");
						var adxq = document.querySelector(".address");
						newedit.style.display = "none";
						adedit.style.display = "block";
						adname.innerHTML = x.aname;
						adphone.innerHTML = x.aphone;
						adxq.innerHTML = x.sheng + x.shi + x.qu + x.addressname;
						var butad = document.querySelector(".button-address");
						var butname = document.querySelector(".button-name");
						var butphone = document.querySelector(".button-phone");
						butad.innerHTML = x.sheng + x.shi + x.qu + x.addressname;
						butname.innerHTML = x.aname;
						butphone.innerHTML = x.aphone;

					}

				})
			}else{
				let newedit = document.querySelector(".order-new-edit");
				let adedit = document.querySelector(".order-edit");
				newedit.style.display = "none";
				adedit.style.display = "block";
			}

			})
	})
}

function clicksheng(index){
	// var c=event.currentTarget.innerHTML;
	console.log((citys[index])["n"]);
	var shi=document.querySelector("#shi");
	let html=``;

	for (var i = 0; i < ((citys[index])["c"]).length; i++) {
		console.log((((citys[index])["c"])[i])["a"])
		html+=`<div class="shi" onclick="clickshi(`+index+`,`+i+`)"  class="sheng" style="background-color: #dddcdc;border-radius: 5px;padding: 10px;">`+(((citys[index])["c"])[i])["n"]+`</div>`
	}
	shi.innerHTML=html;
	var shidiv=document.querySelectorAll(".shi");
	shidiv[0].onclick()
}

function clickshi(a,b){
	console.log((((citys[a])["c"])[b])["n"]);
	var qu=document.querySelector("#qu");
	let html=``;
	for (var i = 0; i < (((citys[a])["c"])[b])["a"].length; i++) {
		console.log()
		html+=`<div onclick="clickqu(`+a+`,`+b+`,`+i+`)"  class="sheng" style="background-color: #dddcdc;border-radius: 5px;padding: 10px;">`+((((citys[a])["c"])[b])["a"])[i]+`</div>`

	}
	qu.innerHTML=html;

}
function clickqu(a,b,c){

	console.log(((((citys[a])["c"])[b])["a"])[c]);
	var nbi = document.querySelector(".nad-body-input2");
	nbi.innerHTML = (citys[a])["n"]+"/"+(((citys[a])["c"])[b])["n"]+"/"+((((citys[a])["c"])[b])["a"])[c];
	var addressselect=document.querySelector("#addressselect");
	addressselect.style.display="none";
}
function selectaddress(){
	var addressselect=document.querySelector("#addressselect");
	addressselect.style.display="block";
}
function liang(){
	let div = document.querySelectorAll(".PaymentMethod");
	div.forEach(a=>{
		a.addEventListener("click",function(){
			div.forEach(x=>{
				x.style.borderColor = "#c5c5c5";
			})
			event.currentTarget.style.borderColor = "#43a1f6";
			payway = event.currentTarget.childNodes[1].value;
		})

	})
}

function shouqi(){
	let parentNode = event.currentTarget.parentNode.parentNode.parentNode;
	let childNode = parentNode.childNodes[3];
	childNode.style.display="none";
	let li4 = event.currentTarget.parentNode.parentNode;
	let li5 = li4.nextElementSibling;
	// let li5 = event.target.parentNode.parentNode.previousElementSibling;
	li4.style.display="none";
	li5.style.display="block";
}

function xiala(){
	let parentNode = event.currentTarget.parentNode.parentNode.parentNode;
	let childNode = parentNode.childNodes[3];
	childNode.style.display="block";
	let li4 = event.currentTarget.parentNode.parentNode;
	li4.style.display="none";
	let li5 = li4.previousElementSibling;
	li5.style.display="block";
}

function daohanglan() {
	let allLi = document.querySelectorAll(".gnb_nav_center li");
	let show = document.querySelector("#show");
	let zzc = document.querySelector("#zzc");
	let img = document.querySelectorAll(".showNavone li span img");
	let showli = document.querySelectorAll(".showNavone li");
	let showNavtwoson = document.querySelector(".showNavtwoson");

	allLi.forEach(x => {
		x.addEventListener("mouseover", function() {
			document.querySelector("#show").style.height = 680 + "px";
			zzc.style.display = "block";
			console.log(document.querySelector("#show").style.height)
			showli.forEach(x => {
				x.style.fontWeight = "";
			})

			for (var i = 0; i < img.length; i++) {
				if (i == 0) {
					img[i].style.display = "block";
					document.querySelector(".showNavone li").style
						.fontWeight = "bold";
				} else {
					img[i].style.display = "none";
				}
			}
			allLi.forEach(y => {
				y.style.background = "#fff";
				y.style.color = "#000";
			})
			event.target.style.background = "#000";
			event.target.style.color = "#ffff";

			showNavtwoson.style.display = "block";
		})

	})
	document.querySelector(".gnb_nav").onmouseleave = function() {
		show.style.height = 0 + "px";
		zzc.style.display = "none";
		allLi.forEach(y => {
			y.style.background = "#fff";
			y.style.color = "#000";
		})
	}

	let cancel = document.querySelector("#cancelshow");
	cancel.addEventListener("click", function() {
		show.style.height = 0 + "px";
	})

	showli.forEach(x => {
		x.addEventListener("mouseover", function() {
			img.forEach(x => {
				x.style.display = "none";
			})
			let hhh = event.currentTarget.querySelector("span img");
			hhh.style.display = "block";
			showli.forEach(y => {
				y.style.fontWeight = "";
			})
			event.currentTarget.style.fontWeight = "bold";

			showNavtwoson.style.display = "none";
			setTimeout(() => {
				showNavtwoson.style.display = "block"
			}, 150);
		})
	})
}
function newaddress(){
	let e = document.querySelector(".nad-body-btn2");
	e.removeEventListener('click',queding2);
	e.onclick = queding;
	let d = document.querySelector(".nad-title-text");
	d.innerHTML = "新建收货地址";
	let a = document.querySelector(".newaddressbox");
	a.style.display = "block";
}
function imgdelbtn(){
	let e = document.querySelector(".nad-body-btn2");
	e.removeEventListener('click',queding2);
	e.onclick = queding;
	let f = document.querySelector(".nad-title-text");
	f.innerHTML = "新建收货地址";
	let a = document.querySelector(".newaddressbox");
	let b = document.querySelectorAll(".nad-body-input1");
	let c = document.querySelector(".nad-body-input2");
	let d = document.querySelector(".nad-body-input3")
	b[0].value = "";
	b[1].value = "";
	c.innerHTML = "选择省份/市区";
	d.value = "";
	a.style.display = "none";
}
function quxiao(){
	let e = document.querySelector(".nad-body-btn2");
	e.removeEventListener('click',queding2);
	e.onclick = queding;
	let a = document.querySelector(".newaddressbox");
	let b = document.querySelectorAll(".nad-body-input1");
	let c = document.querySelector(".nad-body-input2");
	let d = document.querySelector(".nad-body-input3")
	b[0].value = "";
	b[1].value = "";
	c.innerHTML = "选择省份/市区";
	d.value = "";

	a.style.display = "none";
}
function queding(){
	let addressname;
	let sheng;
	let shi;
	let qu;
	let aname;
	let aphone;
	let a = document.querySelectorAll(".nad-body-input1");
	let b = document.querySelector(".nad-body-input2");
	let c = document.querySelector(".nad-body-input3");
	console.log(a[0].value);
	if (a[0].value!=""){
		aname = a[0].value;
	}else{
		alert("请输入正确名字");
		return null;
	}

	if ((/^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/).test(a[1].value)){
		aphone = a[1].value;
	}else{
		alert("请输入正确的手机号");
		return null;
	}
	if (b.innerText!="选择省份/自治区"){
		var strings = b.innerHTML.split("/");
		sheng = strings[0];
		shi = strings[1];
		qu = strings[2];
	}else {
		alert("请选择省市区");
		return null;
	}

	if (c.value!=""){
		addressname= c.value;
	}else{
		alert("请输入详细地址");
		return null;
	}

	$.ajax({
		url: "/orderXq?v=addnewAddress",
		type: "post",
		data: {
			addressname,
			sheng,
			shi,
			qu,
			aname,
			aphone
		},
		success:(r=>{
			let a = document.querySelector(".newaddressbox");
			if (r=="成功添加地址！"){
				alert(r);
				let b = document.querySelectorAll(".nad-body-input1");
				let c = document.querySelector(".nad-body-input2");
				let d = document.querySelector(".nad-body-input3")
				b[0].value = "";
				b[1].value = "";
				c.innerHTML = "选择省份/市区";
				d.value = "";
				$(".cad-addressbox").load(location.href + " .cad-addressbox>*","");
				a.style.display = "none";
			}else{
				alert(r);
			}

		})
	})
}
function queding2(){
	console.log("xxxxxxxxxxxx");
	let addressname;
	let sheng;
	let shi;
	let qu;
	let aname;
	let aphone;
	let jaid = qaid;
	let a = document.querySelectorAll(".nad-body-input1");
	let b = document.querySelector(".nad-body-input2");
	let c = document.querySelector(".nad-body-input3");
	console.log(a[0].value);
	if (a[0].value!=""){
		aname = a[0].value;
	}else{
		alert("请输入正确名字");
		return null;
	}

	if ((/^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/).test(a[1].value)){
		aphone = a[1].value;
	}else{
		alert("请输入正确的手机号");
		return null;
	}
	if (b.innerText!="选择省份/自治区"){
		var strings = b.innerHTML.split("/");
		sheng = strings[0];
		shi = strings[1];
		qu = strings[2];
	}else {
		alert("请选择省市区");
		return null;
	}

	if (c.value!=""){
		addressname= c.value;
	}else{
		alert("请输入详细地址");
		return null;
	}

	$.ajax({
		url: "/orderXq?v=bianjiAddress",
		type: "post",
		data: {
			jaid,
			addressname,
			sheng,
			shi,
			qu,
			aname,
			aphone
		},
		success:(r=>{
			let a = document.querySelector(".newaddressbox");
			if (r=="地址编辑成功！"){
				let e = document.querySelector(".nad-body-btn2");
				e.removeEventListener('click',queding2);
				e.onclick = queding;
				let b = document.querySelectorAll(".nad-body-input1");
				let c = document.querySelector(".nad-body-input2");
				let d = document.querySelector(".nad-body-input3")
				b[0].value = "";
				b[1].value = "";
				c.innerHTML = "选择省份/市区";
				d.value = "";
				alert(r);
				$(".cad-addressbox").load(location.href + " .cad-addressbox>*","");
				a.style.display = "none";
			}else{
				alert(r);
			}
		})
	})

}
function cadquxiao(){
	var a = document.querySelector(".chooseaddressbox");
	a.style.display="none";
}
function cadnewaddress(){
	let e = document.querySelector(".nad-body-btn2");
	e.onclick = queding;
	let d = document.querySelector(".nad-title-text");
	d.innerHTML = "新建收货地址";
	let a = document.querySelector(".newaddressbox");
	a.style.display = "block";
	xiugai();
}
function cadokbtn(){
	cadquxiao();
}
function caddelbtn(){
	let a = event.target.parentNode;
	event.target.parentNode.parentNode.removeChild(a);
	let val =  a.childNodes[1].value;
	console.log(val);
	$.ajax({
		url:"/orderXq?v=delAddress",
		type:"post",
		data:{
			val
		},
		success:function (r){

		}
	})
}
function cadbianjibtn(){
	let a = event.target.parentNode;
	let val =  a.childNodes[1].value;
	$.ajax({
		url:"/orderXq?v=selectOne",
		type:"post",
		data:{
			val
		},
		dataType:"json",
		success:(r)=>{
			newaddress();
			let a = document.querySelectorAll(".nad-body-input1");
			let b = document.querySelector(".nad-body-input2");
			let c = document.querySelector(".nad-body-input3");
			let d = document.querySelector(".nad-title-text");
			a[0].value = r.aname;
			a[1].value = r.aphone;
			b.innerHTML = r.sheng+"/"+r.shi+"/"+r.qu;
			c.value = r.addressname;
			d.innerHTML = "编辑收货地址";
			let e = document.querySelector(".nad-body-btn2");
			console.log(r.aid);
			qaid = r.aid;
			e.onclick = false;
			e.addEventListener('click',queding2);
		}
	})
}

function xiugai(){
	var a = document.querySelector(".chooseaddressbox");
	a.style.display="block";
}

function submitbtn(){
	var input = document.querySelector(".el-checkbox__inner");
	if (payway == null){
		alert("请选择支付方式");
	}else if(!input.checked){
		alert("请勾选隐私协议");
	}else {
		let a = document.querySelector(".agerrment-dtotal");
		let dstatus = "待付款";
		let pay = payway;
		let dtotal = a.value;
		let Aid = document.querySelector(".aid");
		let aid = Aid.value;
		$.ajax({
			url:"/orderXq?v=ordersinsert",
			type:"post",
			data:{
				dstatus,
				pay,
				dtotal,
				aid
			},
			success:(r)=>{
				location.href="/payment";
			}
		})
	}

}




